package com.xsky.servlet;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.xsky.common.util.PBECoder;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;

/**
*
* @author crystal
*/
public class FileUploaded extends HttpServlet 
{

    /**
      * Processes requests for both HTTP <code>GET</code>and<code>POST</code> methods.
      * @param request servlet request
      * @param response servlet response
      */
    // �����ļ����ϴ�·��
// �����ļ����ϴ���С

    private int maxPostSize = 100 * 1024 * 1024;

    public FileUploaded() 
    {
        super();
    }

    public void destroy() 
    {
        super.destroy();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException 
    {
    	String password = (String)request.getParameter("password");
    	BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(32768);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(maxPostSize);
        try 
        {
            List fileItems = upload.parseRequest(request);
            Iterator iter = fileItems.iterator();
            while (iter.hasNext()) 
            {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) 
                {
                	String fileContent = item.getString();
					try
					{
						byte[] decryptFrom = PBECoder.parseHexStr2Byte(fileContent);
						byte[] decryptResult = PBECoder.decrypt(decryptFrom,password);
						fileContent =new String(decryptResult,"utf-8");
					} catch (Exception e1)
					{
						e1.printStackTrace();
					}
                	String xmlStr = fileContent;
                	xmlStr = xmlStr.replace("\r\n", "");
                	xmlStr = xmlStr.substring(xmlStr.indexOf("<data>"));
                	Document docXml = null;
            		try
            		{
            			docXml = DocumentHelper.parseText(xmlStr);
            			Node countProjectNode = docXml.selectSingleNode("//data/CountProject");
            			XStream xs = new XStream(new DomDriver());
            			
            		} catch (DocumentException e)
            		{
            			e.printStackTrace();
            		}
                	
               }
            }
        } 
        catch (FileUploadException e) 
        {
             e.printStackTrace();
            System.out.println(e.getMessage() + "����");
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
      * Handles the HTTP <code>GET</code> method.
      * @param request servlet request
      * @param response servlet response
      */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException 
    {
         processRequest(request, response);
    }

    /**
      * Handles the HTTP <code>POST</code> method.
      * @param request servlet request
      * @param response servlet response
      */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException 
    {
         processRequest(request, response);
    }

    /**
      * Returns a short description of the servlet.
      */
    public String getServletInfo() 
    {
        return "Short description";
    }
    // </editor-fold>

}